<template>
  <div class="agent">
    <div class="agent-nodata" v-if="!hasList">
      <div class="agent-nodata-content">
        <img src="../../assets/agent/nodata.png" alt="">
        <p>还没有内容哦～</p>
      </div>
    </div>
    <div class="agent-content" v-else>
      <div class="agent-search">
        <input type="text" v-model="data.keyword" placeholder="姓名/手机号">
        <div class="agent-search-btn" @click="search"><img src="../../assets/agent/search.png" alt=""></div>
      </div>

      <nut-infiniteloading
        @loadmore="getList"
        :is-show-mod="true"
        :has-more="hasMore"
        :is-loading="loading"
        :threshold="100">
        <div class="agent-row" v-for="(data,index) in agentList" :key="index">
          <div class="agent-row-title">
            {{data.name}}<span>{{data.type_name}}</span>
          </div>
          <div class="agent-row-content">
            <div class="agent-row-item">
              <p>{{data.phone}}</p>
              <span>提成比例：{{data.share_proportion}}%</span>
            </div>
            <div class="agent-row-item">
              <p>设备：{{data.device_num}}</p>
              <span>总收益：{{data.total_money}}元</span>
            </div>
            <div class="agent-row-item">
              <p v-if="data.pid == 0">上级：顶级</p>
              <p v-else>上级：{{data.pid_data.name}}</p>
              <nut-switch :active="data.is_enabled == 0" class="my-switch" @change="changeIsEnabled($event, data.id)">
              </nut-switch>
            </div>
          </div>
          <div class="agent-footer" @click="goShop(data.id)">
            <p>酒店数：{{data.store_num}}</p>
            <img src="../../assets/agent/right.png" alt="">
          </div>
        </div>
      </nut-infiniteloading>
      <img class="addBtn" src="../../assets/agent/add.png" @click="add">
    </div>
  </div>
</template>

<script>
  import { AgentMethod, AgentSetMethod } from '@/utils/request.js'

  export default {
    data() {
      return {
        data: {
          page: 1,
          page_size: this.pagePageSize,
          keyword: ''
        },
        loading: false,
        hasMore: true,
        agentList: [],
        hasList: true
      }
    },
    mounted() {
      document.title = "代理列表"
      this.getList()
    },
    methods: {
      goShop(id) {
        this.$router.push({ path: '/shop', query: { id }})
      },
      add() {
        this.$router.push("/dividend")
      },
      search() {
        this.data.page = 1
        this.agentList = []
        this.getList()
      },
      getList() {
        this.loading = true
        AgentMethod(this.data).then((res) => {
          if (res.code == 200) {
            if (res.data.length == 0) {
              if (this.data.page == 1 && this.data.keyword == '') {
                this.hasList = false
              }
              this.hasMore = false
            } else {
              this.data.page++
              res.data.filter(item => {
                this.agentList.push(item)
              })
            }
          } else {
            this.$toast.fail('数据获取失败!')
          }
          this.loading = false
        })
      },
      changeIsEnabled(status, id) {
        let form = {
          code: status == true ? 0 : 1,
          id: id
        }
        AgentSetMethod(form).then((res) => {
          if (res.code == 200) {
            this.$toast.success('设置成功');
          } else {
            this.$toast.fail('设置失败')
          }
        })
      }
    }
  };
</script>

<style lang="less" scoped>
  .agent {
    .agent-nodata {
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      height: calc(100vh - 88px);

      .agent-nodata-content {
        p {
          padding: 0;
          margin: 0;
          height: 16px;
          font-size: 16px;
          font-family: Adobe Heiti Std;
          font-weight: normal;
          color: #999999;
          text-align: center;
        }

        img {
          width: 245px;
          margin: 0 auto;
        }
      }

    }

    .agent-content {
      box-sizing: border-box;
      padding: 18px 16px;

      .agent-search {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        background: #fff;
        border-radius: 20px;
        height: 33px;
        overflow: hidden;
        background-color: #E5E5E5;

        input {
          height: 33px;
          border: 0;
          outline: none;
          width: 100%;
          padding-left: 20px;
          box-sizing: border-box;
          background: transparent;
        }

        .agent-search-btn {
          min-width: 69px;
          height: 33px;
          display: flex;
          justify-content: center;
          align-items: center;
          background-color: #fff;

          img {
            width: 23px;
          }

        }
      }

      .agent-row {
        margin-top: 11px;
        background: #FFFFFF;
        box-shadow: 0px 0px 9px 0px rgba(51, 51, 51, 0.09);
        border-radius: 5px;
        padding: 0 12px;

        .agent-row-title {
          padding: 17px 0 9px 0px;
          font-size: 16px;
          color: #1677FF;
          font-weight: bold;
          border-bottom: 1px solid #eee;

          span {
            font-size: 12px;
            color: #999999;
            margin-left: 15px;
          }
        }

        .agent-row-content {
          padding: 9px 0;
          border-bottom: 1px solid #eee;
          font-weight: bold;

          .agent-row-item {
            display: flex;
            justify-content: space-between;
            align-items: center;

            color: #333333;
            font-size: 14px;

            p {
              margin: 0;
              padding: 9px 0;
            }
          }
        }

        .agent-footer {
          font-size: 14px;
          font-weight: bold;
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 17px 0;

          p {
            padding: 0;
            margin: 0;
          }

          img {
            width: 20px;
          }
        }
      }

      .addBtn {
        width: 66px;
        height: 66px;
        position: fixed;
        bottom: 70px;
        right: 11px;
        z-index: 6;
      }
    }
  }


  .nut-switch.nut-switch-active {
    background-color: #1677FF;
    border-color: #1677FF;
  }

  .nut-switch-base {
    background-color: #999999;
    border-color: #999999;
  }

  @media screen and (min-width: 300px)and (max-width: 320px) {
    .agent .agent-content .agent-search {
      height: 28px;
    }
    .agent .agent-content .agent-search{
       height: 28px;
    }
    .agent .agent-content .agent-search .agent-search-btn{
      height: 28px;
    }
    .agent .agent-content .agent-search .agent-search-btn img{
      width:19px;
    }
  }
</style>
<style>
  .nut-switch.nut-switch-active .nut-switch-btn {
    background-color: #fff !important;
  }

  .nut-switch .nut-switch-btn {
    background-color: #fff !important;
  }
</style>
